package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;


    @ResponseBody
    @RequestMapping("/showRightTwo")
    public List<RightClassTwo> showRightTwoList(Long id,Long superId){
        List<RightClassTwo> rightClassTwos = accountService.showRightByRightSuperId(id,superId);
        return rightClassTwos;
    }

    @ResponseBody
    @RequestMapping("/deleteAccount")
    public String  deleteAccount(Long id){
        boolean isok = accountService.deleteAccountByUserCode(id);
        if (isok){
            return "true";
        }else {
            return "false";
        }
    }

    @ResponseBody
    @RequestMapping("/showAccount")
    public Account showAccount(Long id){
        Account account = accountService.findAccountByUserCode(id);
        return account;
    }

    @ResponseBody
    @RequestMapping("/findRoleListOnAccount")
    public List<Role> findRoleList(HttpSession session){
        Account thisAccount = (Account) session.getAttribute("thisAccount");
        ArrayList<Role> list = new ArrayList<Role>();
        List<Role> roleList = accountService.findRoleList(thisAccount.getRoleId(),list);
        return roleList;
    }

    @ResponseBody
    @RequestMapping("/updateAccount")
    public String updateAccount(@RequestBody Account upAccount){
        boolean isok = accountService.updateAccount(upAccount);
        if (isok){
            return "true";
        }
        return "false";
    }

    @ResponseBody
    @RequestMapping("/updatePassword")
    public String updatePassword(@RequestBody Account upAccount){
        /*System.out.println(account.getPassWord()+"*****"+account.getUserCode());*/
        boolean isok = accountService.updatePassWord(upAccount);
        if (isok){
            return "true";
        }
        return "false";
    }

    @ResponseBody
    @RequestMapping("/isUserName")
    public String findAccountByUserName(String userName){
        boolean isok = accountService.findAccountByUserName(userName);
        if (isok){
            return "true";
        }
        return "false";
    }

    @ResponseBody
    @RequestMapping("/insertAccount")
    public String insertAccount(@RequestBody Account insAccount){
        insAccount.setCreateDay(new Date());
        boolean isok = accountService.addAccount(insAccount);
        if (isok){
            return "true";
        }
        return "false";
    }

    @ResponseBody
    @RequestMapping("/fuzzySearch")
    public PageInfo<Account> findFuzzySearch(@RequestBody AccountPage accountPage, HttpSession session){
        Account thisAccount = (Account)session.getAttribute("thisAccount");
        Account account = accountPage.getAccount();
        Integer page = accountPage.getPage();
        if (page==null){
            page=1;
        }
        PageHelper.startPage(page,7);
        List<Account> list = new ArrayList<Account>();
        if (thisAccount.getRoleId()==1){
            list = accountService.findAccountListByFuzzy_1(thisAccount.getRoleId(), account);
        }else {
            list = accountService.findAccountListByFuzzy_2(thisAccount.getRoleId(), account);
        }
        PageInfo<Account> pageInfo = new PageInfo<Account>(list, 5);
        List<Account> list1 = pageInfo.getList();
        for (Account a :list1) {
            System.out.println(a);
        }
        return pageInfo;

    }

    /*@ResponseBody
    @RequestMapping("/showAccountList")
    public List<Account> showAccountList(HttpSession session){
        Account thisAccount = (Account)session.getAttribute("thisAccount");
        Account account = accountService.findAccountByUserCode(thisAccount.getUserCode());
        List<Account> accounts = accountService.showAccountByRole(thisAccount.getRoleId());
        accounts.add(0,account);
        return accounts;
    }*/
}
